!function(e,t){function i(e){this.create(e)}var p,r;t.PhotoDropzone=(p=e,(r=t).npm.objectAssign(i.prototype,r.npm.EventEmitter.prototype,{template:psdata_photos_dropzone.template,templatePreview:psdata_photos_dropzone.template_preview,create:function(e){this.uploading=0,this.$el=p(this.template).appendTo(e),this.$photos=this.$el.find(".ps-js-photos"),this.$file=this.$el.find("input[type=file]"),this.$upload=this.$el.find(".ps-js-upload"),this.$upload.on("click",p.proxy(this.onUpload,this)),r.isWebdriver()&&(this.uploadInit(),this.uploadInitialized=!0)},empty:function(e){"save"!==e&&this.removeTempFiles(this.getPhotos()),this.$photos.find(".ps-js-preview").remove(),this.emit("photo_empty")},getPhotos:function(){var t=[];return this.$photos.find(".ps-js-preview").each(function(){var e=p(this).data("id");t.push(e)}),t},upload:function(){this.uploadInitialized||(this.uploadInit(),this.uploadInitialized=!0),this.$file.trigger("click")},uploadInit:function(){var o=peepsodata.currentuserid;this.$file.psFileupload({singleFileUploads:!0,sequentialUploads:!1,replaceFileInput:!0,dropZone:this.$el,pasteZone:null,dataType:"json",url:peepsodata.ajaxurl_legacy+"photosajax.upload_photo",add:p.proxy(this.uploadAdd,this),submit:p.proxy(function(e,t){var i={user_id:o};t.formData=r.observer.applyFilters("photos_upload_req",i,this)},this),progress:p.proxy(this.uploadProgress,this),done:p.proxy(this.uploadDone,this)}),this.on("photo_added",this.uploadAdded),this.on("photo_removed",this.uploadRemoved)},uploadAdd:function(e,t){this.$file=this.$el.find("input[type=file]"),this.validate(t)},uploadAdded:function(e,t){var i=p(this.templatePreview);i.find(".ps-js-progress").css({width:1}),i.on("click",".ps-js-remove",{id:t},p.proxy(this.onRemove,this)),i.addClass("ps-js-preview-"+t),this.$upload.before(i),e.preview=i,this.resetSortable()},uploadRemoved:function(e){var t=this.$photos.find(".ps-js-preview-"+e);t.fadeOut(500,p.proxy(function(){t.remove(),this.$photos.find(".ps-js-preview").length?this.resetSortable():this.emit("photo_empty")},this))},uploadProgress:function(e,t){var i,o=t.preview;i=100*t.loaded/t.total,i=Math.max(0,Math.min(98,i)),i+="%",o.find(".ps-js-progress").stop().animate({width:i},2e3),this.beforeUnloadHandler||(this.beforeUnloadHandler=function(){return!0},r.observer.addFilter("beforeunload",this.beforeUnloadHandler))},uploadDone:function(e,t){var i,o,s=t.preview,a=t.result;this.beforeUnloadHandler&&(r.observer.removeFilter("beforeunload",this.beforeUnloadHandler),this.beforeUnloadHandler=null),a.success?(s.data("id",a.data.files[0]),s.find(".ps-js-img").html(''),s.find(".ps-js-remove").show(),s.find(".ps-js-progress").stop().animate({width:"100%"},1e3,function(){setTimeout(function(){s.find(".ps-js-progressbar").fadeOut()},1e3)})):a.errors&&a.errors.length&&(i=a.errors.join("
"),(o=p("
").append(i).attr("title",i)).css({color:"red",fontSize:".8em",height:"100%",overflow:"hidden",padding:4}),s.find(".ps-js-progress").stop().hide(),s.find(".ps-js-remove").show(),s.find(".ps-js-img").html(o)),this.emit("photo_upload_done",--this.uploading)},validate:function(e){var t=(new Date).getTime()+Math.floor(1e3*Math.random());this.emit("photo_added",e,t),this.emit("photo_upload_start",++this.uploading),this.validateQueue||(this.validateQueue=[]),this.validateQueue.push([e,t]),this._validate()},_validate:_.throttle(function(){if(!this._validateProgress){var e=this.validateQueue.shift();if(e){this._validateProgress=!0;var t=e[0],i=e[1],o=t.files[0];if(!/\.(gif|jpg|jpeg|png|tif|tiff)$/i.test(o.name))return this.emit("photo_upload_done",--this.uploading),this.validateError(i,peepsophotosdata.error_unsupported_format),this._validateProgress=!1,void this._validate();var s=_.filter(this.getPhotos(),function(e){return e}),a=r.observer.applyFilters("photos_validate_req",{size:parseInt(o.size),filesize:parseInt(o.size),photos:s.length+1},this);p.ajax({type:"POST",url:peepsodata.ajaxurl_legacy+"photosajax.validate_photo_upload",data:a,dataType:"json"}).done(p.proxy(function(e){e.success?t.submit().done(p.proxy(function(){setTimeout(p.proxy(function(){this._validateProgress=!1,this._validate()},this),1e3)},this)).fail(p.proxy(function(e){this.emit("photo_upload_done",--this.uploading),this.validateFail(i),this._validateProgress=!1,this._validate()},this)):e.errors&&e.errors.length&&(this.emit("photo_upload_done",--this.uploading),this.validateError(i,e.errors.join("
")),this._validateProgress=!1,this._validate())},this)).fail(p.proxy(function(e){this.emit("photo_upload_done",--this.uploading),this.validateFail(i),this._validateProgress=!1,this._validate()},this))}}},1e3),validateError:function(e,t){var i=this.$photos.find(".ps-js-preview-"+e),o=p("
").append(""+t+"").attr("title",t);o.css({color:"red",display:"table",fontSize:".8em",height:"100%",overflow:"hidden",padding:4}).find("span").css({display:"table-cell",verticalAlign:"middle"}),i.find(".ps-js-progress").stop().hide(),i.find(".ps-js-remove").show(),i.find(".ps-js-img").html(o)},validateFail:function(e){var t=psdata_photos_dropzone.text_upload_failed_notice,i=this.$photos.find(".ps-js-preview-"+e),o=p("
").append(""+t+"").attr("title",t);o.css({color:"red",display:"table",fontSize:".8em",height:"100%",overflow:"hidden",padding:4}).find("span").css({display:"table-cell",verticalAlign:"middle"}),i.find(".ps-js-progress").stop().hide(),i.find(".ps-js-remove").show(),i.find(".ps-js-img").html(o)},resetSortable:function(){this.$photos.sortable({items:".ps-js-preview"})},destroy:function(){this.removeTempFiles(this.getPhotos()),this.$el.remove()},removeTempFiles:function(e){if(_.isArray(e)&&1<=e.length){var t={user_id:peepsodata.currentuserid,photo:e,_wpnonce:this.$el.find("[name=_wpnonce_remove_temp_files]").val(),_wp_http_referer:this.$el.find("[name=_wp_http_referer]").val()};t=r.observer.applyFilters("photos_remove_temp_files",t),r.postJson("photosajax.remove_temp_files",t)}},onUpload:function(){this.upload()},onRemove:function(e){var t=e.data.id,i=this.$photos.find(".ps-js-preview-"+t);this.removeTempFiles([i.data("id")]),this.emit("photo_removed",t)},triggerUpload:function(){this.upload()},addDroppable:function(t){var i=setInterval(p.proxy(function(){if(this.$el.parent()){clearInterval(i),this.uploadInitialized||(this.uploadInit(),this.uploadInitialized=!0);var e=this.$file.psFileupload("option","dropZone");e=e.add(t),this.$file.psFileupload("option","dropZone",e)}},this),1e3)}}),i)}(jQuery,peepso);